$(document).ready(function(){
	$("#startTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true });
	$("#endTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true });
	
	$("#all_button").click(function(){
		window.location="${ctx}/operationlog/list";
	});
	
	
	$(".ip").each(function(index,element){
		if($(element).text()!="-"){
			var thiselement=element;
			$.ajax({
				url: "https://api.map.baidu.com/location/ip",
				type: "GET",
				dataType: "jsonp",
				data: {"ak":"WQBUdviMoptGWH1y0rHyiUaw","ip":$(element).text()},
				success: function (result){
					if(result.status==0){
						$(thiselement).parent().find(".address").html(result.content.address);
					}
		   		}
		   	});
		}
	});
	
	//开始导出
	var onExporting=false;
	var this_timestamp=new Date().getTime();
	$("#export_button").click(function(){
		$('#dialog_export').modal();
		if(!onExporting){
			this_timestamp=new Date().getTime();
			onExporting=true;
			$.get(
				ctx+'/operationlog/export',
				{
					'operating':$("#operating").val(),
					'startTime':$("#startTime").val(),
					'endTime':$("#endTime").val(),
					'timestamp':this_timestamp
				},
				function(result){
					onExporting=false;
					$("#export_process .progress-bar").css("width","100%");
					$("#export_process .progress-bar span").html("100%已完成");
					
					if(null!=process_timeout){
						clearTimeout(process_timeout);
					}
					
					window.location=ctx+"/"+result;
				}
			);
			
			$("#export_process").show();
			$("#export_process .progress-bar").css("width","1%");
			$("#export_process .progress-bar span").html("0%已完成");
			exportProcess();
		}
	});
	
	function exportProcess(){
		$.get(ctx+'/operationlog/exportProcess',{'timestamp':this_timestamp},function(data){
			if(null!=data && data!=""){
				$("#export_process .progress-bar").css("width",parseFloat(data)*100+"%");
				$("#export_process .progress-bar span").html((parseFloat(data)*100).toFixed(1)+"%已完成");
			}
		});
	}
	
	var process_timeout;
	$(document).ajaxSuccess(function (event, xhr, settings) {
	    if(settings.url.indexOf("exportProcess")!=-1){
	    	process_timeout=setTimeout(function(){
	    		exportProcess();
	    	}, 2000);//2s
	    }
	});
		
});

